﻿using System;
using System.ServiceModel;
using TableTop.DataModel;
using TableTop.DataModel.Shelf;

namespace TableTop.TestConsoleApplication.Network
{
    public class ProviderClient
        : IDisposable
    {
        private ChannelFactory<IProviderServerInterface> cfCurrentFactory = null;
        private NetTcpBinding Binding = null;
        private IProviderServerInterface ServerChannel = null;

        public ProviderClient()
        {
            EndpointAddress epaAddress = null;
            this.Binding = new NetTcpBinding();
            this.cfCurrentFactory = new ChannelFactory<IProviderServerInterface>(this.Binding);
            epaAddress = new EndpointAddress(ProviderServer.str_Address);
            this.ServerChannel = this.cfCurrentFactory.CreateChannel(epaAddress);
        }

        public void SendSheet(ICharacterSheetData Sheet)
        {
            TransportableSerializer<ICharacterSheetData> ssSheet = null;

            ssSheet = new TransportableSerializer<ICharacterSheetData>(Sheet);
            this.ServerChannel.GetSerializedSheet(ssSheet);                 
        }

        #region IDisposable Members

        public void Dispose()
        {
            IServiceChannel isc = null;

            isc = (IServiceChannel)this.ServerChannel;
            isc.Close();
            isc = null;
            this.ServerChannel = null;
            this.cfCurrentFactory.Close();
            this.cfCurrentFactory = null;
            this.Binding = null;
        }

        #endregion
    }
}
